<template>
  <div>
    <div class="loginPage" v-show="showLogin">
      <h1>登录</h1>
      <p v-show="showTip" style="color:red">{{tip}}</p>
      <input type="text" placeholder="请输入用户名" class="mobile" v-model="mobile">
      <input type="password" placeholder="请输入密码" class="pwd" v-model="pwd">
      <button class="loginBtn" @click="login">登录</button>
    </div>
  </div>
</template>

<script>
import { setCookie,getCookie,delCookie } from '../assets/cookie.js'
import {url} from "./url.js"
export default {
  name: 'Login',
  data () {
    return {
      showRegister:false,
      showLogin:true,
      showTip:false,
      tip:'',
      mobile: '',
      pwd: '',
      newmobile: '',
      newpwd: ''
    }
  },
  mounted(){
    /*页面挂载获取cookie，如果存在mobile的cookie，则跳转到主页，不需登录*/
    if(getCookie('mobile')){
      this.$router.push('/admin')
    }
  },
  methods:{
    login(){
      var self = this;
      if(self.mobile == ""|| self.pwd == ""){
        self.tip = "请输入用户名或密码"
        self.showTip = true
      }else{
        var self = this;
        var pwd = self.$md5(self.pwd)
        var data = {'mobile':self.mobile,'pwd':pwd}
        // console.log(data)
        self.$http.post(
         url+'/users/session',//服务器
          // 'http://172.16.8.120/users/session',//高
          // 'http://172.16.9.46:7777/users/session',//王
          // 'http://localhost:7777/users/session',
          data,
          // {headers: {
          //   "Access-Control-Allow-Origin": "*",
          // }}
          ).then(
            function (res) {
              // console.log(res)
              // console.log(res.data.data)
              var code = res.data.code;
              var msg  = res.data.msg
              var mobile = res.data.data
              // console.log(msg)
              /*接口的传值是(-1,该用户不存在),(0,密码错误)，同时还会检测管理员账号的值*/
              if(code == 400){
                self.tip = msg
                self.showTip = true
              }else if((code==200)&&(mobile=="admin")){
                // console.log(code)
              /*路由跳转this.$router.push*/
                //self.$router.push('/admin')
                self.tip = msg
                self.showTip = true
                // console.log(mobile)
                setCookie(mobile,'',1000*60)
                setTimeout(function(){
                  self.$router.push('/admin')
                },1000)
              }else if((code==200)&&(mobile !== 'admin')){
                // console.log(code);
                //self.$router.push('/home')
                self.tip = msg
                self.showTip = true
                setCookie(mobile,'',1000*60)
                setTimeout(function(){
                  self.$router.push('/home')
                }.bind(self),1000)
              }
            }).catch(function (error){
      　　  //请求失败
            });
         }
    }
  }
}
</script>

<style scoped>
.lf{float:left}
.rt{float:right}
.loginPage{
  margin:0 auto;
  width:300px;
}
.mobile{
  box-sizing: border-box;
  width: 100%;
  padding: 10px 10px;
  margin: 10px 0;
}
.pwd{
  box-sizing: border-box;
  width: 100%;
  padding: 10px 10px;
  margin: 10px 0;
}
.loginBtn{
  display: block;
  width:100%;
  padding: 10px 10px;
  margin: 10px 0;
  background:green;
  color:#ddd;
  cursor:pointer;
}
.register:hover{
  color:green;
  cursor:pointer;
}
.loginPage .tipclolr{
  color:red;
}
</style>
